#include "volumewidget.h"

volumeWidget::volumeWidget(QString icon)
{
        volPix = new QPixmap(QString(QApplication::applicationDirPath() + "/themes" + icon));
        this->setPixmap(*volPix);

        angle_big = 236;
        p_angle_big = M_PI * angle_big * (this->boundingRect().width() / 2) / 180;
        r = this->boundingRect().width() / 2;
}

void volumeWidget::mousePressEvent ( QGraphicsSceneMouseEvent * event )
{
        int value = computeVolume(event);
        emit volumeChanged(value);
        qDebug() << "Event => volumeChanged(" << value << ")";
}

void volumeWidget::mouseMoveEvent(QGraphicsSceneMouseEvent * event)
{

        int value = computeVolume(event);

        emit volumeChanged(value);
        qDebug() << "Event => volumeChanged(" << value << ")";

        //keep base implementation
        QGraphicsItem::mouseMoveEvent(event);
}

int volumeWidget::computeVolume(QGraphicsSceneMouseEvent * event)
{
        qreal value;
        if(event->pos().x() < this->boundingRect().width() / 2){

                if((event->pos().y() > 55) && (event->pos().y() < this->boundingRect().height() - 30)){

                        qDebug() << "part 1";
                        value =  50 - event->pos().y() * 40 / (this->boundingRect().height() - 55);
                }else{
                        qDebug() << "part 2";
                        value =  40 + (event->pos().x() - this->boundingRect().width() / 4) * 10 / (this->boundingRect().width() / 4);
                }

               // value = p_afinal * 100 / p_angle_big;
        }else{
                if((event->pos().y() > 55) && (event->pos().y() < this->boundingRect().height() - 30)){
                    qDebug() << "part 4";
                    value = 55 + event->pos().y() * 40 / (this->boundingRect().height() - 80);
                }else{
                    qDebug() << "part 3";
                    value =  50 + (event->pos().x() - this->boundingRect().width() / 2) * 10 / (this->boundingRect().width() / 2);
                }

        }

        qDebug() << "value = " << value;
        if(value > 100)
                value = 100;
        if(value < 0)
                value = 0;

        return value;

}
